/**
  * 背包面板
  */
module game {

    export class QianghuaMediator extends BaseMediator {
        public static NAME: string = "QianghuaMediator";

        public constructor(viewComponent: any = null) {
            super(QianghuaMediator.NAME, viewComponent);
        }
        /**
         * 初始化面板ui
         */
        public initUI(): void {
            this.qianghuaPanel.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.closeButtonClick, this);
            this.qianghuaPanel.sendBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.sendMsgButtonClick, this);
        }

        /**
         * 初始化面板数据
         */
        public initData(): void {


        }

        public onRegister() {
            super.onRegister();

            this.addSocketHandler();
        }
        /**
         * 处理客户端-服务器的收发请求
         */
        public addSocketHandler(): void {
            GameMain.me.evNet.addEventListener(ProtoID.GM_ChangeNewGetMahjong_Response, this.gmChangeAllCardRsp, this);
        }

        private gmChangeAllCardRsp(ev: NetEvent) {
            this.qianghuaPanel.updateView(ev.data);
        }
        public listNotificationInterests(): Array<any> {
            return [
                PanelNotify.OPEN_QIANGHUA,
                PanelNotify.CLOSE_QIANGHUA,
                UserInfoNotify.UPDATE_DATA
            ];
        }
        private qianghuaPanel: QianghuaPanel;
        /**
         * 处理其他地方传过来的请求
         */
        public handleNotification(notification: puremvc.INotification): void {
            var data: any = notification.getBody();
            switch (notification.getName()) {
                case PanelNotify.OPEN_QIANGHUA: {
                    if (!this.qianghuaPanel) {
                        Global.showWaritPanel();
                        RES.addEventListener(RES.ResourceEvent.GROUP_COMPLETE, this.onResourceLoadComplete, this);
                        RES.loadGroup("qianghua");
                    } else {
                        this.openui();
                    }
                    // this.openui();

                    break;
                }
                case PanelNotify.CLOSE_QIANGHUA: {
                    this.closePanel(1);
                    break;
                }
                case PanelNotify.GET_DATA:
                    break;
                case UserInfoNotify.UPDATE_DATA: {
                    this.qianghuaPanel.showText.text += "userId:" + data.getUserId() + "\nuserName:" + data.getUserName();
                    break;
                }
            }
        }

        private openui() {
            if (!this.qianghuaPanel) {
                this.qianghuaPanel = new QianghuaPanel();
            }
            //显示角色面板
            this.showUI(this.qianghuaPanel, true, 0, 0, 3);
            this.sendUserInfo(1);//请求窗口数据

        }

        private onResourceLoadComplete(event: RES.ResourceEvent): void {
            Global.hideWaritPanel();
            this.openui();
        }

        private sendMsgButtonClick(event: egret.TouchEvent): void {
            this.sendName();
        }

        private closeButtonClick(event: egret.TouchEvent): void {
            this.closePanel(1);
        }

        private sendUserInfo(num: number) {
            var msg = GameMain.me.proto.msgnew("play.GM_ChangeNewGetMahjong_Request");//协议名都能从proto文件中找到
            msg.mahjong = num;
            SocketManager.me.startSend(msg);
        }
        private sendName() {
            var msg = GameMain.me.proto.msgnew("play.Hu_Request");
            msg.mahjong = 2;
            SocketManager.me.startSend(msg);
        }
    }
}